﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using blackhouse.BlackApart.Business.Apartments;
using blackhouse.BlackApart.Business;
using blackhouse.BlackApart.Business.Places;
using blackhouse.JavaScript.JQuery;
using System.Text;
using System.Globalization;
using blackhouse.Web.FasterFiles;

namespace blackhouse.BlackApart.Web.Modules {
	public partial class ApartmentMap : System.Web.UI.UserControl {

		#region Fields

		private IApartmentGroup group;
		private IPlace[] places;

		#endregion

		#region Properties

		[Browsable(true)]
		[Category("Layout")]
		[DefaultValue(typeof(Unit), "300px")]
		public Unit MapHeight {
			get;
			set;
		}

		[Browsable(true)]
		[Category("Layout")]
		public string Title {
			get;
			set;
		}

		[Browsable(true)]
		[Category("Layout")]
		[DefaultValue("/Data/SiteImages/gmap-star.png")]
		public string IconApartment {
			get;
			set;
		}

		[Browsable(true)]
		[Category("Layout")]
		[DefaultValue(true)]
		public bool ShowTitle {
			get;
			set;
		}

		#endregion

		#region Constructors

		public ApartmentMap() {
			this.MapHeight = new Unit(300, UnitType.Pixel);
			this.IconApartment = "/Data/SiteImages/gmap-star.png";
			this.ShowTitle = true;
		}

		#endregion

		#region Event Handlers

		protected override void OnInit(EventArgs e) {
			base.OnInit(e);

			this.SetInstance();
		}

		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);

			this.AddJavascript();
			this.PopulateControls();
		}

		#endregion

		#region Methods

		private void AddJavascript() {

			ClientScriptManager cs = Page.ClientScript;
			if (!cs.IsClientScriptIncludeRegistered("google-map"))
				cs.RegisterClientScriptInclude("google-map", "http://maps.googleapis.com/maps/api/js?sensor=false");

			JQueryLibraries jql = new JQueryLibraries();
			jql.IncludeFileBasedOnJQuery("blackhouse.BlackApart.Web.JavaScript.apartment-map.js,blackhouse.BlackApart.Web");
			FasterFilesProvider ffp = new FasterFilesProvider();
			ffp.AddAfterScript(this.GetSetJS());
		}

		private string GetSetJS() {
			StringBuilder sb = new StringBuilder();
			sb.AppendLine(@"var $apartmentMap = new ApartmentMap();
                $(function () {");
			sb.AppendFormat("$apartmentMap.SetCanvasMap('{0}');", this.map.ClientID);
			sb.AppendLine();
			sb.AppendFormat("$apartmentMap.SetApartmentIcon('{0}');", this.IconApartment);
			sb.AppendLine();
			sb.AppendFormat(CultureInfo.GetCultureInfo("en-GB"), "$apartmentMap.SetApartment('{0}', '{1}', {2}, {3});", this.group.Name, this.group.DefaultLocalization.LocationName, this.group.DefaultLocalization.Localization.Latitude, this.group.DefaultLocalization.Localization.Longitude);
			sb.AppendLine();

			foreach (IPlace place in this.places) {
				sb.AppendFormat(CultureInfo.GetCultureInfo("en-GB"), "$apartmentMap.SetPlace('{0}', '{1}', '{2}', '{3}', {4}, {5});", place.Category, place.Name, place.Description, place.Address, place.Location.Latitude, place.Location.Longitude);
				sb.AppendLine();
			}
			sb.AppendLine("$apartmentMap.Populate();});");
			string js = sb.ToString();
			js = js.ReplaceEx("\r", "").ReplaceEx("\n", "");
			return js;
		}

		private void SetInstance() {
			this.places = Factory.Instance().GetPlace().GetAllPlaces();
			if (!string.IsNullOrEmpty(Request["groupId"])) {
				int groupId = 0;
				if (Int32.TryParse(Request["groupId"], out groupId)) {
					this.group = Factory.Instance().GetApartmentGroup(groupId);
					return;
				}
			}

			this.group = DisplayApartmentTypeState.GetCurrentState().ApartmentType;
		}

		private void PopulateControls() {
			if (!string.IsNullOrEmpty(this.Title))
				this.ltlName.Text = this.Title;
			this.pnlTitle.Visible = this.ShowTitle;

			this.map.Height = this.MapHeight;
		}

		#endregion

	}
}